public class ListNode {
    public int val;
    public ListNode next;
    public ListNode head;
    public ListNode(int a) {
        val = a;
    }
    //删除key的所有元素
    public void removeAll(int key){
        ListNode cur=head.next;
        ListNode Pre=head;

        if(this.head==null){
            return ;
        }
        while(cur!=null){
            if(cur.val==key){
                Pre.next=cur.next;
                cur=cur.next;
            }else{
                Pre=Pre.next;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }
    //翻转链表
    public ListNode reverseList() {
    if(head==null){
        return null;
    }
    if(head.next==null){
        return head;
    }
    ListNode cur=head;
    cur=head.next;
    while(cur!=null) {
        ListNode curNext = cur.next;
        cur.next = head;
        cur = curNext;
    }
     return head;
    }
    //返回中间结点
    public ListNode midNode(){
        ListNode fast =head;
        ListNode slow =head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }
    //返回倒数第K个结点
    public ListNode findNode(int k){
        if(k<0||head==next){
            return null;
        }
        ListNode fast =head;
        ListNode slow =head;
        while(k-1!=0){
            fast=fast.next;
        }
        while(fast!=null&&fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
    //回文
  public boolean chkPalindrome() {
        if(head==null){
            return false;
        }
        //找到中间位置
      ListNode fast =head;
      ListNode slow =head;
      while (fast!=null&&fast.next!=null){
          fast=fast.next.next;
          slow=slow.next;
      }
      //翻转
      ListNode cur=slow.next;
      while(cur!=null){
          ListNode curNext=cur.next;
          cur.next=slow;
          slow=cur;
          cur=curNext;
      }
      //比较
      while (head!=slow){
          if (head.val!=slow.val){
              return false;
          }
          if(head.next==slow){
              return true;
          }
          head=head.next;
          slow=slow.next;
      }
      return true;

    }

}
